/*
 * Copyright Aristool AG Switzerland
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
dojo.require("dijit.layout.SplitContainer");
dojo.require("dijit.layout.ContentPane");
dojo.provide("opprua.widget.AutoLayout");

opprua.autolayout = function(comp) {
  comp.subs = {};
  
  comp.getSubcomponents = function() {
    if(this.subs) return this.subs;
    return {};
  }
  
  comp.install = function(parent) {
    parent.setContent(this.getLayout("JavaScript/opprua/widget/templates/autolayout.html"));
    this.top = this.getWidget(this.id + "top");
    this.left = this.getWidget(this.id + "left");
    this.right = this.getWidget(this.id + "right");
    
    this.top.startup();
    comp.setDisplay();
  }

  comp.setRightData = function(data) {
    var d = this.getData(data);
    var w = opprua.dumpObject(d, data).domNode;
    w.style.marginLeft = "20px";
    w.style.marginTop = "20px";
    this.right.setContent(w);
  }
  
  comp.callServer = function(req) {
    var r = new opprua.request(req.entry, document.req, null, null);
    if(req.component) {
      r.requestComponent(req.component, this, this.installSub);
    }
    r.send();
  }  
  
  comp.installSub = function(comp) {
    var s = this.subs;
    if(! s) {
      s = this.parent.subs;
    }
    if(s[comp.name]) return;
    if(comp.decorator != "opprua.autolayout") {
      /**
       teaser on the left, component on the right, to be implemented
       var r = dojo.widget.getWidgetById("root");
       var n = opprua.registry.createComponent(component);
       n.install(r);
       opprua.registry.setRoot(n);
      */
    } else {
      var c = opprua.registry.createComponent(comp);
      c.left = this.left;
      c.right = this.right;
      c.parent = this;
      c.setDisplay();
      s[comp.name] = c;
    }
  }

  comp.setRightRequest = function(req) {    
    var w = document.createElement("div");
    document.req = {};
    
    w.appendChild(document.createTextNode(req.entry)); 
    w.style.marginLeft = "10px";
    w.style.marginTop = "20px";
    w.style.width = "95%";

    var t = this.buildMask(req.mask.entry, "document.req");
    t.style.marginLeft = "20px";
    t.style.marginTop = "5px";
    t.id = "inputMask";
    w.appendChild(t);

    t = document.createElement("div");
    t.appendChild(document.createTextNode("submit"));
    t.style.marginTop="5px";
    t.className = "oppruaautolink";
    var c = new opprua.autoCaller(this, this.callServer, req);
    t.onclick = dojo.hitch(c, c.call);
    w.appendChild(t);
    
    this.right.setContent(w);
  }
  
  comp.buildMask = function(mask, collector) {
    var s = "<table style='width:100%'>";
    
    for(i in mask) {
      s += "<tr><td align='right' width='1px'>" + mask[i].name + 
           "</td><td style='width:10px'>:</td><td><input onchange='" + 
           collector + "." + mask[i].name + "=this.value;' style='width:100%;'></td></tr>";
    }
    s += "</table>";
    var e = document.createElement("div");
    e.innerHTML = s;
    return e;
  }

  comp.setDisplay = function() {
    
    var b = new opprua.autoBox(this.left.domNode, this.description.name);
    b.vert(10);
    b.addText(0, "Data in component:", null);
    b.vert(5);
    
    var d = this.getDataNames();
    for(i in d) {
      b.addText(0, d[i], new opprua.autoCaller(this, this.setRightData, d[i]));
    }
    
    b.vert(10);
    b.addText(0, "Links in component:", null);
    b.vert(5);

    d = this.getRequests();
    for(i in d) {
      b.addText(0, d[i].entry, new opprua.autoCaller(this, this.setRightRequest, d[i]));
    }    
  }
}

opprua.autoCaller = function(ob, func, param) {
  this.ob = ob;
  this.func = func;
  this.parameter = param;
  
  this.call = function() {
    this.func.call(this.ob, this.parameter);
  };
}

opprua.autoBox = function(parent, name) {
  
  this.tease = document.createElement("div");
  this.tease.innerHTML = name;
  this.tease.className = "oppruaautolink";
  this.tease.style.marginTop = "15px";
  this.tease.style.marginLeft = "20px";
  this.tease.style.fontSize = "16px";
  this.tease.style.fontWeight = "bold";
  parent.appendChild(this.tease);
  
  this.holder = document.createElement("div");
  this.holder.style.marginLeft = "40px";
  parent.appendChild(this.holder);
  
  this.toggle = function() {
    this.holder.style.display = this.holder.style.display == "none" ? "block" : "none";
  }

  this.tease.onclick = dojo.hitch(this, "toggle");

  this.addText = function(indent, text, caller) {
    var div = document.createElement("div");
    div.appendChild(document.createTextNode(text));
    div.style.marginLeft=indent + "px";
    if(caller) {
      div.className = "oppruaautolink";
      div.onclick = dojo.hitch(caller, caller.call);
    }
    this.holder.appendChild(div);
  }
  
  this.vert = function(dis) {
    var div = document.createElement("div");
    div.style.height=dis + "px";
    this.holder.appendChild(div);
  }
}